package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exceptionResolver.ExceptionMessage;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkItem")
public class CheckItemController {
    @Reference
    private CheckItemService checkItemService;

    @RequestMapping("/add")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @ExceptionMessage(value = MessageConstant.ADD_CHECKITEM_SUCCESS)
    public Result add(@RequestBody CheckItem newCheckItem) {
            checkItemService.add(newCheckItem);
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkItemService.pageQuery(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @ExceptionMessage(value = MessageConstant.DELETE_CHECKITEM_FAIL)
    public Result delete(@PathVariable("id") int id) {
            checkItemService.delete(id);
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findById/{id}")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @ExceptionMessage(value = MessageConstant.QUERY_CHECKITEM_FAIL)
    public Result findById(@PathVariable("id") int id) {
        CheckItem checkItem = null;
            checkItem = checkItemService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
    }

    @RequestMapping("/update")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @ExceptionMessage(value = MessageConstant.EDIT_CHECKITEM_FAIL)
    public Result update(@RequestBody CheckItem checkItem) {
            checkItemService.update(checkItem);
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findAll")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @ExceptionMessage(value = MessageConstant.QUERY_CHECKITEM_FAIL)
    public Result findAll(){
        List<CheckItem> checkItemList=null;
           checkItemList= checkItemService.findAll();
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
    }

}
